<?php
namespace Admin\Model;
use Think\Model;

class projectImpPlanModel extends AbstractModel {

	protected $results = array();
	
	public $_validate = array(
        array('ProjectID','number','请选择项目名称',Model::MUST_VALIDATE),
        array('WorkItem','require','请选择工作事项',Model::MUST_VALIDATE),
        array('DptID','number','请选择负责单位',Model::MUST_VALIDATE)
    );
	
	/*
	protected $_auto = array(
    	array('ProjectNO', 'getNowNO',3,'callback')
  	);
    */
	
	public function Myupdata(){
		if(!$this->create()){
			$this->results['msg'] = $this->getError();
			$this->results['state'] = 0;
		}else{
			$res = $this->save();
			//echo $this->getLastSql();
			if($res) {
				$this->results['state'] = 1;
				$this->results['msg'] = "修改成功！";
				$this->results['id'] = $res;
			}else{
				$this->results['state'] = 0;
				$this->results['msg'] = "修改错误！";
			}
		}
		return $this->results;
	}
	
	
	public function Myshow($parmArr){
		$where = " 1 ";
		foreach($parmArr AS $key => $value){
			$where .= " AND $key = '$value'"; 
		}
		$sql = "SELECT * FROM project_imp_plan WHERE $where";
		$res = $this->query($sql);
		
		if($res[0]['DptID']){
			$DptIDsql = "SELECT Name FROM department WHERE ID in({$res[0]['DptID']})";
			$DptIDres = $this->query($DptIDsql);
			$DptName = "";
			foreach($DptIDres[0] AS $k => $v){
				$DptName .= $v.",";
			}
		}
		
		if($res[0]['OtherDpt']){
			$OtherDptsql = "SELECT Name FROM department WHERE ID in({$res[0]['OtherDpt']})";
			$OtherDptres = $this->query($OtherDptsql);
			$OtherDptName = "";
			foreach($OtherDptres AS $k => $v){
				$OtherDptName .= $v['Name'].",";
			}
		}
		
		$res[0]['DptName'] = substr($DptName, 0, -1);
		$res[0]['OtherDptName'] = substr($OtherDptName, 0, -1);
		return $res;
	}
	
	public function MyshowList(){
		//$WorkItemArr = array('','准备阶段','启动阶段','实施阶段','验收阶段');
		$where = " 1 ";
		$keyword = I('keyword');
		/*
		if(!empty($keyword)){
			$sql = "SELECT ID FROM project WHERE Name = '{$keyword}'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND ProjectID = ".$res[0]['ID'];
			}else{
				$where .= " AND ProjectID = -1";
			}
		}
		*/
		if(!empty($keyword)){
			$sql = "SELECT ID FROM department WHERE Name LIKE '%{$keyword}%'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND DptID = ".$res[0]['ID'];
			}
		}
		
		$id = (int)I('id');
		if(!empty($id)){
			$where .= " AND ProjectID = ".$id; 
		}
		$return = array();
		$sql = " SELECT COUNT(*) count FROM project_imp_plan WHERE $where ";
		$count = $this->query($sql);
		$count = $count[0]['count'];
	    $page = $this->Mypage($count, 20);
		$sql = "SELECT *,(SELECT Name FROM project WHERE ID = a.ProjectID) AS ProjectName, (SELECT Name FROM department WHERE ID = a.DptID) AS DptName FROM project_imp_plan AS a WHERE $where ORDER BY ID DESC LIMIT  $page->firstRow , $page->listRows";
		$res = $this->query($sql);
		$return['list'] = $res;
		$return['page'] = $page->show('Admin');
		return $return;
	}
	
	public function getProjectList(){
		$depat = M("project");
		//$where['IsDel'] = 0;
		return $depat->field('ID,Name')->where()->select();
	}
	
	public function getTableData(){
		$dataArr = array();
		$i = 1;
		$j = 1;
		$k = 1;
		$l = 1;
		$join = " LEFT JOIN department AS b ON a.DptID = b.ID";
		$res1 = $this->alias('a')->field('a.*,b.Name AS DptName,b.ID AS DptID')->join($join)->where(array("ProjectID"=>$_GET['id']))->order('ID DESC')->select();
		foreach($res1 AS $k => $v){
			$arr1 = array();
			$OtherDptName = "";
			if(!empty($v['OtherDpt'])){
				$where['ID'] = array("IN",$v['OtherDpt']);
				$where['IsDel'] = 0;
				$res2 = M("department")->field('Name')->where($where)->select();
				if(!empty($res2)){
					foreach($res2 AS $K => $V){
						$OtherDptName.= $V['Name'].',';
					}
					$OtherDptName = substr($OtherDptName,0,-1);
				}
			}
			if(!empty($v['StartTime']) && !empty($v['EndTime'])){
				$strtotime = (strtotime($v['EndTime']) - strtotime($v['StartTime']));
				$continued = ((int)$strtotime / (60*60*24)) >= 0 ? (int)$strtotime / (60*60*24) : 0;
			}
			$arr1[0]['StartTime'] = $v['StartTime'];
			$arr1[1]['EndTime'] = $v['EndTime'];
			$arr1[2]['continued'] = (int)$continued;
			$arr1[3]['OutputContent'] = $v['OutputContent'];
			$arr1[4]['DptName'] = $v['DptName'];
			$arr1[5]['OtherDptName'] = $OtherDptName;
			$arr1[6]['ID'] = $v['ID'];
			$arr1[7]['DptID'] = $v['DptID'];
			switch($v['WorkItem']){
				case 1:
						$dataArr['data'][0]['Getready'][$i]['JSON'] = $arr1;
						$i++;
					break;
				case 2:
						$dataArr['data'][1]['startup'][$j]['JSON'] = $arr1;
						$j++;
					break;
				case 3:
						$dataArr['data'][2]['Implementations'][$k]['JSON'] = $arr1;
						$k++;
					break;
				case 4:
						$dataArr['data'][3]['check'][$l]['JSON'] = $arr1;
						$l++;
					break;
				default:
					break;
			}
		}
		$res2 = M("project")->field("Name,ID")->where(array("ID"=>$_GET['id']))->find();
		$dataArr['projectName'] = $res2['Name'];
		$dataArr['projectID'] = $res2['ID'];
		return $dataArr;
	}

	// 甘特图 
public function MyshowGantt(){
		//$WorkItemArr = array('','准备阶段','启动阶段','实施阶段','验收阶段');
		$where = " 1 ";
		$keyword = I('keyword');
		/*
		if(!empty($keyword)){
			$sql = "SELECT ID FROM project WHERE Name = '{$keyword}'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND ProjectID = ".$res[0]['ID'];
			}else{
				$where .= " AND ProjectID = -1";
			}
		}
		*/
		if(!empty($keyword)){
			$sql = "SELECT ID FROM department WHERE Name LIKE '%{$keyword}%'";
			$res = $this->query($sql);
			if(!empty($res)){
				$where .= " AND DptID = ".$res[0]['ID'];
			}
		}
		
		$id = (int)I('id');
		if(!empty($id)){
			$where .= " AND ProjectID = ".$id; 
		}
		$return = array();
		//$sql = " SELECT COUNT(*) count FROM project_imp_plan WHERE $where ";
		//$count = $this->query($sql);
		//$count = $count[0]['count'];
	    //$page = $this->Mypage($count, 20);
		$sql = "SELECT *,(SELECT Name FROM project WHERE ID = a.ProjectID) AS ProjectName, (SELECT Name FROM department WHERE ID = a.DptID) AS DptName FROM project_imp_plan AS a WHERE $where ORDER BY ID DESC";
		$res = $this->query($sql);
		
		$WorkItemArr = array('','准备阶段','启动阶段','实施阶段','验收阶段');
		foreach ($res as $key => $value) {
			$newdata['name']=$WorkItemArr[$value['WorkItem']];//$value['ProjectName'];
			// /$newdata['desc']=$WorkItemArr[$value['WorkItem']];
			$newdata['values']=array();
			$newd['t']['from']='/Date('.strtotime($value['StartTime']).'000)/';
			$newd['t']['to']='/Date('.strtotime($value['EndTime']).')/000';
			//$newd['t']['desc']=$WorkItemArr[$value['WorkItem']];
			$newd['t']['label']='进度';
			$newd['t']['customClass']='计划进度';
			$newdata['values']='['.json_encode($newd['t'],JSON_UNESCAPED_SLASHES).']';
			//var_dump($newdata['value']); exit();
			$arr[]=$newdata;
		}
		//exit();
		$return['list'] = $res;
		$return['data'] = $arr;
		//$return['page'] = $page->show('Admin');
		return $return;
	}

	//返回13位时间戳
	private function getMillisecond() { 
	list($t1, $t2) = explode(' ', microtime()); 
	return (float)sprintf('%.0f',(floatval($t1)+floatval($t2))*1000); 
	} 


}




?>				